<?php
class customersSession extends spModel
{
    private $data = null;
    private $session_id = null;
    private $minutes_to_expire = 86400; // TIME TO MAINTAIN DATA ON DB

    public function __construct()
    {
        global $SESSION;

        //检查当前终端是否有ID，没有就新建一个ID
        if (isset($_COOKIE['abcsession_id']))
        {
            $this->session_id = $_COOKIE['abcsession_id'];
        }
        $condition = array('session_id' => $this->session_id);
        $query = spClass('SessionModel')->find($condition);
        if (!$query)
        {
            $this->session_id = md5(microtime() . rand(1, 9999999999999999999999999)); // GENERATE A RANDOM ID
            setcookie('abcsession_id', $this->session_id,time()+$this->minutes_to_expire,WEBDIR);
            $data = array('session_id' => $this->session_id,'updated_on'=>time());
            spClass('SessionModel')->create($data);
        }
        //调用清除过期记录函数
        $this->expire();
    }

    //清除过期记录
    private function expire()
    {
        $date_to_delete = time() - $this->minutes_to_expire;
        $conditions = 'updated_on<=' . $date_to_delete;
        spClass('SessionModel')->delete($conditions); // 删除记录
    }
    //清除过期记录
    public function del()
    {

        $date_to_delete = time() - $this->minutes_to_expire;
        //echo  $this->session_id;
        $conditions = array('session_id' => $this->session_id);
        spClass('SessionModel')->delete($conditions); // 删除记录
    }

    //写入修改记录
    public function write($session_data, $session_time=86400)
    {
        $time2=$this->minutes_to_expire;
        if($session_time>$time2){
            $ttime=$session_time-$time2;
            //echo "aa".$ttime;
            $data2 = array('value' => $session_data, 'updated_on' => time()+$ttime);
        }else{
        $data2 = array('value' => $session_data, 'updated_on' => time());
        //echo "aa".$ttime;
        }
        //dump($data2);
        $condition = array('session_id' => $this->session_id);
        spClass('SessionModel')->update($condition, $data2);
        setcookie('abcsession_id', $this->session_id,time()+$session_time,WEBDIR);
        //exit;
    }
    public function read()
    {
        // your code
        $condition = array('session_id' => $this->session_id);
        $query = spClass('SessionModel')->find($condition);
        //dump($query);

        if ($query)
            return $query['value'];
        return '';

    }
    public function cartlist()
    {
        $list = array();
        $tmpArray = array();
        $customers=$this->read();
        $customers=unserialize($customers);
        //dump($customers);
        $condition = array('customers_id' => $customers['customers_id']);
        $list = spClass('customers_basketModel')->findAll($condition);
        if (is_array($list))
        {
            //dump($list);
            foreach ($list as $key => $val)
            {
                $tmpArray['id'][$val['customers_basket_id']] = $val['customers_basket_id'];
                $tmpArray['pid'][$val['customers_basket_id']] = $val['products_id'];
                $tmpArray['name'][$val['customers_basket_id']] = $val['products_name'];
                $tmpArray['size'][$val['customers_basket_id']] = $val['attributes'];
                $tmpArray['price1'][$val['customers_basket_id']] = $val['final_price'];
                $tmpArray['price2'][$val['customers_basket_id']] = $val['discounted_price'];
                $tmpArray['count'][$val['customers_basket_id']] = $val['customers_basket_quantity'];
            }
        }
        return $tmpArray;
    }
}
?>